package cn.kgc.auth.controller;

import cn.kgc.auth.service.UserService;
import cn.kgc.dto.Dto;
import cn.kgc.mapper.UserMapper;
import cn.kgc.pojo.User;
import cn.kgc.vo.IlxToken;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.mobile.device.Device;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;

/**
 * 文档注释 生成 帮助文件
 * RestController  == Controller  +  ResponseBody
 *          返回的是JSON
 */
@RestController  //
@RequestMapping("/api/")
public class UserController {
    @Resource
    private UserService userService;

    //判断邮箱/手机  是否已经存在
    @GetMapping("ckusr")
    public Dto isExists(String name){
        return  userService.isExist(name);
    }

    //邮箱注册功能
    @PostMapping("doregister")
    public Dto registerByEmail(@RequestBody User user){
        return userService.register(user);
    }

    //邮箱/手机激活
    @PutMapping("activate")
    public Dto activeUser(String user,String code){
        return userService.activeUser(user,code);
    }

    //手机注册
    @PostMapping("registerByPhone")
    public Dto registerbyphone(@RequestBody User user) {
        return  userService.registerByphone(user);
    }

    //用户登录--》》请求来源 http请求中的requestHeader 来话获取请求来源
    @PostMapping("dologin")
    public  Dto<IlxToken> doLogin(String name, String password,Device device){
        String from="pc";
        if (!device.isNormal()){
            from="mobile";
        }
        return userService.selectByLogin(name,password,from);
    }
    @GetMapping(value = "logout",headers = "token")
    public Dto logout(HttpServletRequest request){
        String token=request.getHeader("token");
        return userService.logout(token);
    }
    @PostMapping(value = "retoken",headers = "token")
    public Dto retoken(HttpServletRequest request){
        String token=request.getHeader("token");
        return userService.reToken(token);
    }
}
